import { Graph, Point } from '@antv/x6'

const eclipse = {
  register: () => {
    Graph.unregisterRouter('eclipse')
    Graph.registerRouter('eclipse', (vertices, args, edgeView) => {
      const points = vertices.map((p) => Point.create(p))
      const source = edgeView.sourceBBox.getCenter()
      const target = edgeView.targetBBox.getCenter()
      const threeBox = 45 // 上下外线位置
      const sourceOutX = Math.max((source.x + target.x) / 2, source.x + 15)
      let sourceOutY = source.y - threeBox
      const targetOutX = target.x - 30
      points.push(new Point(sourceOutX, source.y))
      if (target.x - source.x > 33) { // 右上 右下
        points.push(...[new Point(sourceOutX, target.y)])
      } else { // 左上 左下
        if (target.y - source.y > 89 || source.y - target.y > 89) { // 非碰撞
          sourceOutY = (source.y + target.y) / 2
        } else {
          sourceOutY = target.y + ((target.y > source.y) ? threeBox : -threeBox) // 左上或左下出口
        }
        points.push(...[new Point(sourceOutX, sourceOutY), new Point(targetOutX, sourceOutY), new Point(targetOutX, target.y)])
      }
      return points
    })
  }
}
export default eclipse
